﻿layui.use(['form', 'layer', 'element'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        element = layui.element;

    $(".coursefile").show();
    $(".courseexam").hide();

    form.on("submit(submitUpload)", function (data) {
        var files = $("#CourseFile")[0].files;
        //var coverImageFile = $("#CourseCoverImage")[0].files[0];
        var loading = layer.load(0, {
            shade: true,
            time: 2 * 1000
        });
        $.each(files, function (i, file) {
            AjaxFile(file, 0);
        });
    });

    form.on("radio(CourseTypeFilter)", function (data) {
        if (data.value == 0) {
            $(".coursefile").show();
            $(".courseexam").hide();
        } else if (data.value == 1) {
            $(".coursefile").hide();
            $(".courseexam").show();
        }
    });

    function AjaxFile(file, i) {
        var name = file.name, size = file.size, shardSize = 2 * 1024 * 1024, shardCount = Math.ceil(size / shardSize);
        if (shardCount <= 0) {
            return;
        }

        //每一片的起始与结束位置
        var start = i * shardSize, end = Math.min(size, start + shardSize);

        var form = new FormData();
        //form.append("TaskId", $(".TaskId").val());
        //form.append("TaskName", $(".TaskId option:selected").text());
        //form.append("CategoryName", $(".CategoryName").val());
        //form.append("CategoryDescription", $(".CategoryDescription").val());
        //form.append("CourseName", $(".CourseName").val());
        //form.append("CourseDescription", $(".CourseDescription").val());
        form.append("FileData", file.slice(start, end));
        form.append("FileName", name);
        form.append("FileTotal", shardCount);
        form.append("FileIndex", i + 1);
        $.ajax({
            type: "POST",
            url: "/Assets/Add/",
            data: form,
            dataType: "json",
            async: false,
            processData: false,
            contentType: false,
            headers: {
                "X-CSRF-TOKEN-Gerald": $("input[name='AntiforgeryKey_Gerald']").val()
            },
            beforeSend: function () {
                layer.load();
            },
            success: function (result) {
                var alertIndex;
                if (result != null && i < shardCount - 1) {
                    i++;
                    AjaxFile(file, i);
                } else {
                    alertIndex = layer.alert(result.ResultMsg, { icon: 1 });
                }
                //loading = layer.load(0, { shade: true, time: 2 * 1000 });
            },
            end: function () {
                layer.closeAll('loading');
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(JSON.stringify(data));
                console.log(JSON.stringify(data));
                layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
            }
        })
    }

    function ShowLoading(t) {
        if (t) {
            console.log(t);
            layer.load(0, { shade: [0.1, '#fff'] });
        } else {
            console.log("关闭loading层:" + t);
            layer.closeAll('loading');
        }
    }

    function open_progress() {
        var content = '<div class="layui-progress layui-progress-big" lay-filter="progress"  lay-showPercent="yes">\n' +
            '  <div class="layui-progress-bar layui-bg-blue" lay-percent="" id="progress"></div>\n' +
            '</div>';
        layer.open({
            type: 1,
            title: false,
            closeBtn: 0,
            resize: false,
            area: ['800px', '18px'],/*这里要加入弹层的宽高，然后一个灰色的进度条就出现在弹层的顶部*/
            content: content,
            success: function (layero, index) {
                element.init();
            }
        });
    }

    function render(index, total) {
        $("#progress").attr("lay-percent", index + '/' + total);
        element.init();
    }
});